package com.bstek.demo.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bstek.demo.entity.MyPage;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Criterion;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.data.provider.filter.FilterCriterion;
import com.bstek.dorado.data.provider.filter.FilterOperator;
import com.bstek.dorado.data.provider.filter.SingleValueFilterCriterion;

public class CommonUtils {
	
	/**
	 * 将mybatis-plus分页查询结果解析分装到dorado分页对象中以便前端展示
	 * @param page dorado分页对象
	 * @param myPage 自定义myPage对象
	 */
	public static <T> Page<T> setPageParams(Page<T> page,MyPage<T> myPage) {
		page.setEntityCount((int) myPage.getTotal());
		page.setEntities(myPage.getRecords());
		return page;
	}
	
	/**
	 * 将dorado封装的分页信息对象转化为自定义分页对象
	 * @author lemon
	 * @param <T>
	 * @param <T>
	 * @return myPage 返回自定义分页对象
	 */
	
	public static  <T> MyPage<T> getPageParams(Page<T> page){
		Integer offset = ((page.getPageNo()) - 1) * page.getPageSize();
		MyPage<T> myPage = new MyPage<>(offset,page.getPageSize());
		return myPage;
	}
	/**
	 * 将Dorado封装的过滤查询信息对象按照Class类型，取出过滤条件，封装在map中以便mybatis使用
	 * @author lemon
	 * @param criteria dorado封装的过滤信息对象
	 * @param clazz 类型
	 */
	public static Map<String,List<String[]>> getFilterParamUtils(Criteria criteria,Class clazz){
		Map<String,List<String[]>> result = new HashMap<>();
		if(criteria != null && clazz != null ){
			List<Criterion> filterParams = criteria.getCriterions();
			Field[] fields = clazz.getDeclaredFields();
			for(Criterion criterion : filterParams){
				// 获取查询的属性名
				String property = ((FilterCriterion)criterion).getProperty();
				for(Field field : fields){
					if(field.getName().equals(property)){
						// 获取过滤栏中的过滤值
						Object value = ((SingleValueFilterCriterion)criterion).getValue();
						// 获取过滤栏中的过滤符号
						FilterOperator filterOperator = ((SingleValueFilterCriterion)criterion).getFilterOperator();
						System.out.println(field.getName()+","+filterOperator.toString()+","+value.toString());
						// like处理
						String valueStr = "";
						String filterOperatorStr = "";
						if("like*".equals(filterOperator.toString())){
							valueStr = value.toString()+"%";
							filterOperatorStr = "like";
						}else if("*like".equals(filterOperator.toString())){
							valueStr = "%"+value.toString();
							filterOperatorStr = "like";
						}else if("like".equals(filterOperator.toString())){
							valueStr = "%"+value.toString()+"%";
							filterOperatorStr = "like";
						}else{
							valueStr = value.toString();
							filterOperatorStr = filterOperator.toString();
						}
						// 一列多筛选条件的处理
						if(result.get(field.getName())!=null){
							List<String[]> params = (List<String[]>)result.get(field.getName());
							String[] param = {filterOperator.toString(),valueStr};
							params.add(param);
							result.put(field.getName(), params);
						}else{
							List<String[]> params = new ArrayList<>();
							String[] param = {filterOperator.toString(),valueStr};
							params.add(param);
							result.put(field.getName(), params);
						}
						break;
					}
				}
			}
		}
		return result;
	}
}
